package com.zenithmind.mall.pojo.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 订单支付信息实体 - 遵循单一职责原则，专门管理支付信息
 * 
 * @author ZenithMind Team
 * @since 2025-07-03
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("mall_order_payment")
public class OrderPayment extends BaseEntity {

    /**
     * 订单ID
     */
    private String orderId;

    /**
     * 支付方式（1-支付宝，2-微信，3-余额，4-银行卡）
     */
    private Integer paymentType;

    /**
     * 支付状态（0-未支付，1-已支付，2-支付失败，3-已退款，4-部分退款）
     */
    private Integer paymentStatus;

    /**
     * 支付金额
     */
    private BigDecimal paymentAmount;

    /**
     * 实际支付金额
     */
    private BigDecimal actualPaymentAmount;

    /**
     * 支付时间
     */
    private LocalDateTime paymentTime;

    /**
     * 第三方支付流水号
     */
    private String transactionId;

    /**
     * 第三方支付订单号
     */
    private String thirdPartyOrderNo;

    /**
     * 支付失败原因
     */
    private String failureReason;

    /**
     * 退款金额
     */
    private BigDecimal refundAmount;

    /**
     * 退款时间
     */
    private LocalDateTime refundTime;

    /**
     * 退款流水号
     */
    private String refundTransactionId;

    /**
     * 退款原因
     */
    private String refundReason;

    // 业务方法

    /**
     * 检查是否未支付
     */
    public boolean isUnpaid() {
        return Objects.equals(this.paymentStatus, 0);
    }

    /**
     * 检查是否已支付
     */
    public boolean isPaid() {
        return Objects.equals(this.paymentStatus, 1);
    }

    /**
     * 检查是否支付失败
     */
    public boolean isPaymentFailed() {
        return Objects.equals(this.paymentStatus, 2);
    }

    /**
     * 检查是否已退款
     */
    public boolean isRefunded() {
        return Objects.equals(this.paymentStatus, 3);
    }

    /**
     * 检查是否部分退款
     */
    public boolean isPartiallyRefunded() {
        return Objects.equals(this.paymentStatus, 4);
    }

    /**
     * 检查是否支付宝支付
     */
    public boolean isAlipay() {
        return Objects.equals(this.paymentType, 1);
    }

    /**
     * 检查是否微信支付
     */
    public boolean isWechatPay() {
        return Objects.equals(this.paymentType, 2);
    }

    /**
     * 检查是否余额支付
     */
    public boolean isBalancePay() {
        return Objects.equals(this.paymentType, 3);
    }

    /**
     * 检查是否银行卡支付
     */
    public boolean isBankCardPay() {
        return Objects.equals(this.paymentType, 4);
    }

    /**
     * 标记为已支付
     */
    public void markAsPaid(BigDecimal actualAmount, String transactionId) {
        this.paymentStatus = 1;
        this.actualPaymentAmount = actualAmount;
        this.transactionId = transactionId;
        this.paymentTime = LocalDateTime.now();
    }

    /**
     * 标记为支付失败
     */
    public void markAsPaymentFailed(String reason) {
        this.paymentStatus = 2;
        this.failureReason = reason;
    }

    /**
     * 执行退款
     */
    public void refund(BigDecimal amount, String reason, String refundTransactionId) {
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }

        BigDecimal currentRefund = this.refundAmount != null ? this.refundAmount : BigDecimal.ZERO;
        BigDecimal totalRefund = currentRefund.add(amount);
        
        this.refundAmount = totalRefund;
        this.refundTime = LocalDateTime.now();
        this.refundReason = reason;
        this.refundTransactionId = refundTransactionId;

        // 判断是全额退款还是部分退款
        if (totalRefund.compareTo(this.actualPaymentAmount) >= 0) {
            this.paymentStatus = 3; // 已退款
        } else {
            this.paymentStatus = 4; // 部分退款
        }
    }

    /**
     * 获取可退款金额
     */
    public BigDecimal getRefundableAmount() {
        if (!isPaid() && !isPartiallyRefunded()) {
            return BigDecimal.ZERO;
        }
        
        BigDecimal paid = actualPaymentAmount != null ? actualPaymentAmount : BigDecimal.ZERO;
        BigDecimal refunded = refundAmount != null ? refundAmount : BigDecimal.ZERO;
        
        return paid.subtract(refunded);
    }

    /**
     * 检查是否可以退款
     */
    public boolean canRefund() {
        return (isPaid() || isPartiallyRefunded()) && 
               getRefundableAmount().compareTo(BigDecimal.ZERO) > 0;
    }

    /**
     * 获取支付方式名称
     */
    public String getPaymentTypeName() {
        if (paymentType == null) return "未知";
        
        switch (paymentType) {
            case 1: return "支付宝";
            case 2: return "微信支付";
            case 3: return "余额支付";
            case 4: return "银行卡";
            default: return "未知";
        }
    }

    /**
     * 获取支付状态名称
     */
    public String getPaymentStatusName() {
        if (paymentStatus == null) return "未知";
        
        switch (paymentStatus) {
            case 0: return "未支付";
            case 1: return "已支付";
            case 2: return "支付失败";
            case 3: return "已退款";
            case 4: return "部分退款";
            default: return "未知";
        }
    }

    /**
     * 验证支付信息数据完整性
     */
    public boolean isValid() {
        return orderId != null && !orderId.trim().isEmpty() &&
               paymentType != null && paymentType >= 1 && paymentType <= 4 &&
               paymentStatus != null && paymentStatus >= 0 && paymentStatus <= 4 &&
               paymentAmount != null && paymentAmount.compareTo(BigDecimal.ZERO) > 0;
    }

    /**
     * 检查支付金额是否一致
     */
    public boolean isAmountConsistent() {
        if (!isPaid()) {
            return true;
        }
        
        return actualPaymentAmount != null && 
               paymentAmount.compareTo(actualPaymentAmount) == 0;
    }
}
